package com.qingke.qixi;

import java.util.regex.Pattern;

import com.qingke.qixi.command.Command;
import com.qingke.qixi.command.CommandCode;
import com.qingke.qixi.command.CommandFactory;
import com.qingke.qixi.pojo.User;


public class App 
{
	private static App instance = null;
	public static synchronized App getInstance(){
		if(instance==null){
			instance = new App();
		}
		return instance;
	}
	
	private static User currentUser=null;
    public User getCurrentUser() {return currentUser;}
	public void setCurrentUser(User currentUser) {App.currentUser = currentUser;}

	public static void main( String[] args ) {
		Console.println("=====开始=====");
		Console.printHelp();
		while(true){
			Command command = getCmd();
			if(command==null){
				continue;
			}
			command.execute();
		}
    }
	
	public static Command getCmd(){
		String cmd=null;
		Pattern pattern = Pattern.compile("[0-9a-zA-Z]+");
		while (true) {
			cmd = Console.askInput("cmd");
			if(!pattern.matcher(cmd).find()){
				Console.println("别乱摁，好好输入");
				continue;
			}
			break;
		}
		CommandCode cmdCode = null;
		try {
			cmdCode=CommandCode.valueOf(cmd.toUpperCase());
		} catch (Exception e) {
			Console.println("命令输入有误，请重新输入");
			return null;
		}
		return new CommandFactory().buildCommand(cmdCode);
	}
}
